#include "clientnetworkstatusdialog.h"
#include "ui_clientnetworkstatusdialog.h"
#include "singleclient.h"

ClientNetworkStatusDialog::ClientNetworkStatusDialog(boost::shared_ptr<SingleClient>client, QWidget *parent):
    QDialog(parent),
	ui(new Ui::ClientNetworkStatusDialog),
	m_Client(client)
{
    ui->setupUi(this);
	setupWidgets();
	setupSignals();
}

void ClientNetworkStatusDialog::setupWidgets()
{
	ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
	ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
	ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);

	ui->tableWidget->setItem(0,0, new QTableWidgetItem(m_Client->getIpAddress()));
	ui->tableWidget->setItem(1,0, new QTableWidgetItem(QString::number(m_Client->getTcpPort())) );
	ui->tableWidget->setItem(2,0, new QTableWidgetItem("0"));
	ui->tableWidget->setItem(3,0, new QTableWidgetItem(m_Client->getRealIpAddress()));
}

void ClientNetworkStatusDialog::setupSignals()
{
	connect(m_Client.get(), SIGNAL(onlineStateChanged(bool)), this, SLOT(onClientOnlineStatusChanged()));
	connect(m_Client.get(), SIGNAL(delayChanged(ulong)), this, SLOT(onDelayChanged(ulong)));
}

void ClientNetworkStatusDialog::onClientOnlineStatusChanged()
{
	ui->tableWidget->item(0,0)->setText(m_Client->getIpAddress());
	ui->tableWidget->item(1,0)->setText(QString::number(m_Client->getTcpPort()));
}

void ClientNetworkStatusDialog::onDelayChanged(unsigned long newDelay)
{
	ui->tableWidget->item(2,0)->setText(QString::number(newDelay));
}

ClientNetworkStatusDialog::~ClientNetworkStatusDialog()
{
    delete ui;
}

void ClientNetworkStatusDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
